$(function(){
	//提示工具
	$("[data-toggle='tooltip']").tooltip();
	
	//导航栏设置
	var rank=parseInt($("html").attr("data-rank"));
	$(".project-navbar a").eq(rank).addClass("current-view").siblings().removeClass("current-view");

	/*----------------------------------------------------------
	 * #now-priority-modal，#now-progress-modal下拉选择框
	 ---------------------------------------------------------*/
	//1、收起选项，取消选项标志类名selected
	//#now-priority-modal，#now-progress-modal下
	$(".modal .close,.modal .cancel").click(function(){
		var $parent=$(this).parents(".modal").find(".options");
		$parent.hide().find(".options-item").removeClass("selected");
	});
	
	//2、收起展开选项
	$(".modal .btn-unfold").click(function(){
		$(this).parents(".modal").find(".options").toggle();
	});
	
	//3、点击具体选项时
	$(".select-box").on("click",".options-item",function(){
		$(this).addClass("selected").siblings().removeClass("selected");
		
		var value,txt;
		value=$(this).attr("value");
		txt=$(this).text();

		$(this).parents(".select-box").find(".btn-unfold span").attr("value",value).html(txt);
	});
	
	/**调整当前优先级**/
	$("#now-priority-modal .confirm").click(function(){
		var unit = $('#changUnit').attr('value');
		var projectId = $('#projectId').val();
		$.post(
				'/projects/changeProjectUnit',
				{'proId' : projectId, 'unitId' : unit},
				function(data){
	                if(data.status == true){
						//window.location.href = "/projects/projectDetail/"+projectId
						//将.btn-audit下的span的文本设为修改后的进度名
						var txt=$("#now-priority-modal .btn-unfold").text();
						$("#priority").text(txt);
						
						//收起选项，并关闭该模态框
						var $parent=$(this).parents(".modal").find(".options");
						$parent.hide().find(".options-item").removeClass("selected");
						$("#now-priority-modal").modal("hide");
	                }
				},'json'
		);
		
	});
	
	
	/**修改当前进度**/
	$("#now-progress-modal .confirm").click(function(){
		var progressId = $('#changPrograss').attr('value');
		var projectId = $('#projectId').val();
		$.post(
				'/projects/changeProjectProgress',
				{'proId' : projectId, 'progressId' : progressId},
				function(data){
	                if(data.status == true){
						//window.location.href = "/projects/projectDetail/"+projectId
						//将.btn-audit下的span的文本设为修改后的进度名
						var txt=$("#now-progress-modal .btn-unfold").text();
						$("#progress").text(txt);
						
						//收起选项，并关闭该模态框
						var $parent=$(this).parents(".modal").find(".options");
						$parent.hide().find(".options-item").removeClass("selected");
						$("#now-progress-modal").modal("hide");
	                }
				},'json'
		);
	});
	
	
	/*--------------------------------------------------
	 * 添加跟进人
	---------------------------------------------------*/

	/**
	 * 1、查找跟进人
	 */
	$("#search-person").on("input",function(){
		var keyWord = $('#search-person').val();
		if(keyWord!=null&&keyWord!=''){
			$.post(
					'/projects/invitations',
					{
						'keyWord' : keyWord
					},
					function(data){
						if(data.status == true){
							var html = "";
							$.each(data.data.data, function(i, obj){
								html += "<p class='clearfix'>"
									 + '<span data-value="'+obj.id+'">'
									 + obj.name
									 + '</span>'
									 + "<span class='add pull-right'>"
									 + "<i class='iconfont icon-icon-xinjian'></i>"
									 + "添加</span>"
									 + '</p>';
							});
							$(".followChoose").html(html).show();
							
						}else{
							$('.followChoose').empty();
						}
					},'json'
			);
		}else{
			$('.followChoose').hide().empty();
		}
	});
	
	//2、添加跟进人函数
	function addPeople(id,name){
		if(!(id=="" || id==null)){
			var flag=true,
				arr=$(".followInput");
			//如果重复，返回flag=false。
			arr.each(function(){
				if($(this).data('value')==id){
					//alert("跟进人已经存在");
					return flag=false;
				}
			});
			//不重复，则添加新跟进人
			if(flag){
				var $new,$name,$del;
				//将新增跟进人插进已选择跟进人列表中
				$new=$("<span class='people pull-left'></span>");
				$name=$("<i class='name followInput' data-value='"+id+"'></i>");
				$del=$("<i class='iconfont icon-shanchu del'><i>");
				$name.html(name);
				$name.appendTo($new);
				$del.appendTo($new);
				$new.appendTo($("#add-people-modal .selected"));
			}
		}
	};
	
	//从搜索结果中添加跟进人
	$("#add-people-modal").on("click",".add",function(){
		var name,chooseId;
		name=$(this).prev("span").text();
		chooseId = $(this).prev("span").attr("data-value");
		addPeople(chooseId,name);
	});
	
	//从推荐中添加跟进人
	$("#add-people-modal .hot-item").on("click","span",function(){
		var name,chooseId;
		name=$(this).text();
		chooseId = $(this).attr("data-value");
		console.log(name+ " " + chooseId);
		addPeople(chooseId,name);
	});
	
	//3、删除已添加的跟进人
	$("#add-people-modal").on("click",".del",function(){
		delTag($(this),"span.people","#add-people-modal .selected");
	});
	
	//删除标签函数
	function delTag(obj,parent,parents){
		//获取直接父级元素span
		var $parent=obj.parent(parent);
		//为父级添加标志类on
		$parent.addClass("on").siblings().removeClass("on");
		//删除含on类的span元素
		$(parents).find("span.on").remove();
	};
	
	//4、保存
	$("#add-people-modal .save").click(function(){
		var projectId = $('#projectId').val();
		var arrays = new Array();
		$('.followInput').each(function(i){
			var array = [];
			var userId = $(this).data('value');
			arrays.push({'userId': userId, 'proId': projectId});
		});
        $.ajax({
            url: "/projects/changeFollow",
            type: "POST",
            contentType : 'application/json', //设置请求头信息
            dataType:"json",
            data: JSON.stringify(arrays),  //将Json对象序列化成Json字符串，JSON.stringify()原生态方法
            success: function(data){
                if(data.status == true){
					window.location.href = "/projects/projectDetail/"+projectId
                }
            },
            error: function(res){
                //alert(res.responseText);
            }
        });
		//关闭弹框
		$('#add-people-modal').modal('hide');
	});
	
});
